package com.fenghongzhang.day016;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<Music> list;

    private Context context;

    public MyAdapter(List<Music> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.music_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.item_artist =  convertView.findViewById(R.id.item_artist);
            viewHolder.item_duration = convertView.findViewById(R.id.item_duration);
            viewHolder.item_title = convertView.findViewById(R.id.item_title);
            viewHolder.item_img = convertView.findViewById(R.id.item_img);
            convertView.setTag(viewHolder);
        }else{
           viewHolder = (ViewHolder) convertView.getTag();
        }
        Music music = list.get(position);
//        viewHolder.item_artist.setText(music.getArtist());
        //格式化时间
//        viewHolder.item_duration.setText(MusicUtil.fmtDate(music.getDuration()));
//        viewHolder.item_duration.setText(music.getDuration());
//        viewHolder.item_img
        //添加专辑封面即可
//        Glide.with(context).load(MusicUtil.getPic(music.getAlbumId(),context)).into(viewHolder.item_img);

        viewHolder.item_title.setText(music.getTitle());
        return convertView;
    }

   class ViewHolder {
        ImageView item_img;
        TextView item_title;
        TextView item_duration;
        TextView item_artist;
    }


}
